<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/4/26 0026
 * Time: 10:55
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\AdminValidate;
use think\Config;
use think\Request;

/**
 * ------------------------------------------
 * Class Admin
 * @package app\admin\controller
 * 管理员管理类
 * ------------------------------------------
 */
class Admin extends Check{
    public function adminList(){
        $admin_model = new Data('admin');
        $username = input('param.username');
        if(!empty($username)){
            $where['a.username'] = ['like',"%{$username}%"];
            $display['username'] = $username;
        }
        $where['a.id'] =['<>',1];
        $join = [['auth_group_access b','a.id=b.uid','left'],['auth_group c','b.group_id = c.id','left']];
        $fields ="a.*,c.title ";
        $result = $admin_model->joinData('a',$join,$where,$fields);
        return view('adminList',['result'=>$result,'display'=>$display]);
    }

    /**
     * 添加管理员
     * @return array|\think\response\View
     */
    public function adminAdd(){
        $character_model = new Data('auth_group');
        try{
            if(Request::instance()->isPost()){
                $group_model     = new Data('auth_group_access');
                $admin_model     = new Data('admin');
                $admin_validate  = new AdminValidate();
                $post = Request::instance()->post();
                $admin_rule = $admin_validate->adminValidate();
                self::validateData($admin_rule['rule'],$admin_rule['message'],$post);
                $data['username'] = $post['username'];
                $data['password'] = password_hash($post['password'],true); //密码加密
                $data['create_time'] = time();
                $data['status'] = $post['status'];
                $admin_model->startTrans(); //开启事务
                $admin_add_id = $admin_model->insertData($data);
                $group_add_bool = $group_model->_insert(['uid'=>$admin_add_id,'group_id'=>$post['group_id']]);
                if(!$group_add_bool || !$admin_add_id){
                    $admin_model->rollback();
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                $admin_model->commit();
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $result = $character_model->selectData();
        return view('adminAdd',['result'=>$result]);
    }

    /**
     * @return array|\think\response\View
     * 修改管理员
     */
    public function adminSave(){
        $id = intval(input('param.id'));
        $admin_model = new Data('admin');
        $group_model = new Data('auth_group');
        $acc_model   = new Data('auth_group_access');
        if(Request::instance()->isPost()){
            $group_id = intval(input('param.group_id'));
            $uid = intval(input('param.uid'));
            $rs = $acc_model->updateData(['group_id'=>$group_id],'uid ='.$uid);
            if($rs === false){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
            return self::formatSuccessResult();
        }
        $join = [['auth_group_access b','a.id = b.uid','left']];
        $fields = "a.*,b.* ";
        $user_one = $admin_model->joinData('a',$join,'a.id = '.$id,$fields);
        $result = $group_model->selectData();
        return view('adminSave',['result'=>$result,'user_one'=>$user_one]);
    }

    /**
     * @return array
     * 改变管理员状态
     */
    public function changeAdminStatus(){
        try{
            $satus = intval(input('param.status'));
            $id    = intval(input('param.id'));
            $admin_model = new Data('admin');
            $status = $satus == 1 ? 2 : 1;
            $rs = $admin_model->updateData(['status'=>$status],'id ='.$id);
            if($rs===false){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
            return self::formatSuccessResult(['status'=>$status]);
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
    }

    /**
     * @return array|\think\response\View
     * 修改管理员密码
     */
    public function adminChangePassword(){
        $id = intval(input('param.id'));
        $admin_model= new Data('admin');
        try{
            if(Request::instance()->isPost()){
                $uid = intval(input('param.id'));
                $password = password_hash(input('param.password'),true);
                $rs = $admin_model->updateData(['password'=>$password],'id='.$uid);
                if($rs === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $admin_info = $admin_model->find('id ='.$id);
        return view('adminChangePassword',['adminInfo'=>$admin_info]);
    }
}